﻿$(document).ready(function () {
    $("#hdnGroupId").val($("#detailGroupId").val());
    $("#hdnSubGroupId").val($("#detailSubGroupId").val());
    registerPaginateClick();
    $("a.detail").click(handleClickDetail);
});

var handleClickDetail = function () {
    var productInfoUrl = "/Detail/Info";
    $("form#contentForm").attr("action", productInfoUrl);
    var index = $(this).attr("index");
    $("#infoGroupId").val($("#groupId" + index).val());
    $("#infoSubGroupId").val($("#subGroupId" + index).val());
    $("#infoProductId").val($("#productId" + index).val());
    //return false;
    $("form#contentForm").submit();
}

function registerPaginateClick() {
    $("p.pagination a").click(function () {
        if ($(this).hasClass("disabled") || $(this).hasClass("selected")) {
            return false;
        }

        $.ajax({
            type: "POST",
            url: "/Detail/Paginate",
            async: false,
            data: {
                pageIndex: $("#pageIndex").val(),
                groupId: $("#hdnGroupId").val(),
                subGroupId: $("#hdnSubGroupId").val()
            },
            dataType: "json",
            success: function (data) {
                if (data != null) {
                    reloadPage(data);
                    $(window).scrollTop($(document).height());
                }
            }
        });
    });
}

function reloadPage(data) {
    if (data == null) {
        return false;
    }

    var listProducts = data.ProductProfile;
    var pageMove = data.GroupName + "&nbsp;&gt;&nbsp;" + data.SubGroupName;
    var pageTitle = data.SubGroupName;

    $("title").text(pageTitle);
    $(".content_banner").remove();
    $(".box_center").empty();
    $(".box_center").append("<ul />");
    $(".content_title h3").html(pageMove);

    for (var i = 0; i < listProducts.length % 4; i++) {
        listProducts.push(null);
    }

    for (var i = 0; i < listProducts.length; i++) {
        var li = $('<li class="product" />');
        var boxSp = $('<div class="boxsp" />');

        // Remove border right
        if ((i + 1) % 4 == 0) {
            $(boxSp).addClass("border-right-none");
        }

        if (listProducts[i] != null) {
            var image = $('<p><a><img src="' + listProducts[i].ImageName + '" alt="' +
                listProducts[i].Name + '" /></a></p>');
            var name = $('<h4 class="productname">' + listProducts[i].Name + '</h4>');
            var price = $('<p class="productprice">Giá: ' + numeral(listProducts[i].Price).format('0,0') + ' VNĐ</p>');
            var description = $('<p class="productdescription">' + listProducts[i].Description + '</p>');
            var detailLink = $('<a class="button detail" index="' + i + '">Chi tiết</a>');
            var hdnProductId = $('<input type="hidden" id="productId' + i
                + '" value="' + listProducts[i].id + '" />');
            var hdnGroupId = $('<input type="hidden" id="groupId' + i
                + '" value="' + listProducts[i].GroupId + '" />');
            var hdnSubGroupId = $('<input type="hidden" id="subGroupId' + i
                + '" value="' + listProducts[i].SubGroupId + '" />');

            $(boxSp).append(image);
            $(boxSp).append(name);
            $(boxSp).append(price);
            $(boxSp).append(description);
            $(boxSp).append(detailLink);
            $(boxSp).append(hdnProductId);
            $(boxSp).append(hdnGroupId);
            $(boxSp).append(hdnSubGroupId);
        }

        $(li).append(boxSp);
        $(".box_center ul").append(li);
    }

    $(".box_center").append($('<div class="content_height10"></div>'));
    // Create paginate
    $("#totalPage").val(data.PageCount);
    $("#pageIndex").val(data.CurrentPage);
    paginate("pagination", parseInt($("#totalPage").val()), parseInt($("#pageIndex").val()));
    registerPaginateClick();
    $("a.detail").click(handleClickDetail);
}